package com.rest.client.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalculatorActivity extends Activity {
	
	private ApplicationLWJ _App;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculator);
		_App = (ApplicationLWJ) getApplication();

		TextView tvWelcome = (TextView) findViewById(R.id.tvWelcome);
		tvWelcome.setText("Welcome " + _App.getUser());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calculator, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	private String contentString = "";
	private String numString = "";
	private double lastValue = 0;
	private double currValue = 0;
	private String lastOpration = "";
	private Boolean equalCalled = false;
	private final String Left_Opration = "+-*/";

	public void click_CalcuButton(View view) {
		Button btn = (Button) view;
		String op = btn.getText().toString();

		try {
			if (Left_Opration.contains(op)) {
				if (!equalCalled && !lastOpration.isEmpty() && Left_Opration.contains(lastOpration)) {
					lastValue = clacu(lastValue, currValue, lastOpration);
					contentString = String.valueOf(lastValue);
					equalCalled = false;
				} else if (!numString.isEmpty()) {
					lastValue = Double
							.valueOf(numString.endsWith(".") ? numString + "0"
									: numString);
					currValue = 0;
				} else if (equalCalled) {
					equalCalled = false;
				}

				lastOpration = op;
				numString = "";
			} else if (op.equals("=")) {
				lastValue = clacu(lastValue, currValue, lastOpration);
				contentString = String.valueOf(lastValue);
				numString = "";
				equalCalled = true;
			} else {
				if (op.equals(".")) {
					if (numString.isEmpty()) {
						numString = "0.";
					} else if (numString.contains(".")) {
						Log.i("Demo", "More than one '.'");
						return;
					} else {
						numString += op;
					}
				} else {
					numString += op;
					currValue = Double.valueOf(numString);
				}
				contentString = numString;
			}
			EditText txtContent = (EditText) findViewById(R.id.txtContent);
			txtContent.setText(contentString);
		} catch (Exception ex) {
			Log.i("Demo", "Exception: " + ex.getMessage());
			EditText txtContent = (EditText) findViewById(R.id.txtContent);
			txtContent.setText(ex.getMessage());

			contentString = "0";
			numString = "";
			lastValue = 0;
			currValue = 0;
			lastOpration = "";
		}
	}

	private double clacu(double first, double second, String opration) {
		
		if (opration.equals("+")) {
			return first + second;
		} else if (opration.equals("-")) {
			return first - second;
		} else if (opration.equals("*")) {
			return first * second;
		} else if (opration.equals("/")) {
			return first / second;
		} else {
			return 0;
		}
	}

	public void click_Clear(View view) {
		contentString = "0";
		numString = "";
		lastValue = 0;
		currValue = 0;
		lastOpration = "";
		EditText txtContent = (EditText) findViewById(R.id.txtContent);
		txtContent.setText(contentString);
	}
}
